package com.infinit.selfhelp.common.net.converter

import com.drake.net.convert.JSONConvert
import com.squareup.moshi.Moshi
import org.json.JSONObject
import java.lang.reflect.Type
import javax.inject.Inject

class MoshiConverter @Inject constructor(
    private val moshi: Moshi,
) : JSONConvert(code = "errorCode", message = "errorMsg") {
    override fun <R> String.parseBody(succeed: Type): R? {
        val string = try {
            JSONObject(this).getString("data")
        } catch (e: Exception) {
            this
        }
        return moshi.adapter<R>(succeed).fromJson(string)
    }
}
